/*
@title: Treasure-Hunt-Adventure
@description: Treasure-Hunt-Adventure is a puzzle game where the player takes on the role of a pirate navigating jungles and beaches to transport a gem back to their ship. Players must strategically move boxes to assist in guiding the gem to its destination.
@author: Sophia E
@tags: ['puzzle']
@addedOn: 2024-04-05
First time? Check out the tutorial game:
https://sprig.hackclub.com/gallery/getting_started

*/

const player = "p";
const block = "b";
const gem = "g";
const groundfloor = "f";
const ship = "s";
const groundsoil = "r";
const sand = "d";
const sandtosoil = "t";
const crate = "c";
const oceanBridge = "o";

const mainMelody = tune `
236.2204724409449: C5^236.2204724409449 + A4^236.2204724409449,
236.2204724409449: C5^236.2204724409449 + A4^236.2204724409449,
236.2204724409449: B4^236.2204724409449 + G4^236.2204724409449,
236.2204724409449: C5^236.2204724409449 + A4^236.2204724409449,
236.2204724409449: D5^236.2204724409449 + B4^236.2204724409449,
236.2204724409449: E5^236.2204724409449 + C5^236.2204724409449 + G5^236.2204724409449,
236.2204724409449: E5^236.2204724409449 + C5^236.2204724409449 + G5^236.2204724409449,
236.2204724409449: F5^236.2204724409449 + B4^236.2204724409449,
236.2204724409449: E5^236.2204724409449 + A4^236.2204724409449,
236.2204724409449: D5^236.2204724409449 + G4^236.2204724409449,
236.2204724409449: E5^236.2204724409449 + A4^236.2204724409449,
236.2204724409449: B4^236.2204724409449 + E5^236.2204724409449,
236.2204724409449: B4^236.2204724409449 + E5^236.2204724409449,
236.2204724409449: A5^236.2204724409449 + D5^236.2204724409449,
236.2204724409449: A5^236.2204724409449 + D5^236.2204724409449,
236.2204724409449: G4^236.2204724409449 + E4^236.2204724409449 + C4~236.2204724409449,
236.2204724409449: G4^236.2204724409449 + E4^236.2204724409449 + C4~236.2204724409449,
236.2204724409449: E5^236.2204724409449 + C5^236.2204724409449,
236.2204724409449: E5^236.2204724409449 + C5^236.2204724409449,
236.2204724409449: D5^236.2204724409449 + B4^236.2204724409449,
236.2204724409449: E5^236.2204724409449 + C5^236.2204724409449,
236.2204724409449: F5^236.2204724409449 + D5^236.2204724409449,
236.2204724409449: G5^236.2204724409449 + E5^236.2204724409449,
236.2204724409449: G5^236.2204724409449 + E5^236.2204724409449,
236.2204724409449: A5^236.2204724409449 + D5^236.2204724409449,
236.2204724409449: G5^236.2204724409449 + C5^236.2204724409449,
236.2204724409449: F5^236.2204724409449 + B4^236.2204724409449,
236.2204724409449: G5^236.2204724409449 + C5^236.2204724409449,
236.2204724409449: G5^236.2204724409449 + D5^236.2204724409449,
236.2204724409449: G5^236.2204724409449 + D5^236.2204724409449,
236.2204724409449: E4^236.2204724409449 + G4^236.2204724409449 + C4~236.2204724409449,
236.2204724409449: E4^236.2204724409449 + G4^236.2204724409449 + C4~236.2204724409449`;
const gemSound = tune `
163.48773841961852,
81.74386920980926: D5-81.74386920980926 + F5~81.74386920980926,
81.74386920980926: D5-81.74386920980926 + F5~81.74386920980926,
81.74386920980926: F5-81.74386920980926 + A5~81.74386920980926,
81.74386920980926: F5-81.74386920980926 + A5~81.74386920980926,
81.74386920980926: B5-81.74386920980926,
2043.5967302452316`;
const winTune = tune `
148.5148514851485,
148.5148514851485: E5-148.5148514851485 + G5^148.5148514851485,
148.5148514851485: F5-148.5148514851485 + A5^148.5148514851485,
148.5148514851485: G5-148.5148514851485 + B5^148.5148514851485,
148.5148514851485,
148.5148514851485: E5-148.5148514851485 + G5^148.5148514851485,
148.5148514851485: F5-148.5148514851485 + A5^148.5148514851485,
148.5148514851485: G5-148.5148514851485 + B5^148.5148514851485,
148.5148514851485,
148.5148514851485: G5^148.5148514851485 + E5-148.5148514851485,
148.5148514851485: A5^148.5148514851485 + F5-148.5148514851485,
148.5148514851485: B5^148.5148514851485 + G5-148.5148514851485,
148.5148514851485,
148.5148514851485: E5-148.5148514851485 + G5^148.5148514851485,
148.5148514851485: F5-148.5148514851485 + A5^148.5148514851485,
148.5148514851485: G5-148.5148514851485 + B5^148.5148514851485,
148.5148514851485,
148.5148514851485: E5-148.5148514851485 + G5^148.5148514851485,
148.5148514851485: F5-148.5148514851485 + A5^148.5148514851485,
148.5148514851485: G5-148.5148514851485 + B5^148.5148514851485,
1782.1782178217823`;
const playback = playTune(mainMelody, Infinity);

setLegend(
  [ player, bitmap`
.3C..6666666....
.33C600000006...
..33C00000006...
..633C000000066.
..0000000000000.
...00CCCCCC00...
...0C02CC02C0...
...0C00CC00C0...
...0CCCCCCCC0...
...0000000000...
...0336226330...
..030362263030..
..0C06222260C0..
...0011111100...
....00L000L0....
....000..000....`],
  [ block, bitmap `
44DD44DD444DDD44
DDDDD4DDDDDDD44D
4DDD444DDD4D444D
D4DDD4DDDD444DDD
4DDDDDD4DD4DDDD4
4DD44DD44DDD4DDD
4DDD4D444DD444DD
D44DDDD4DDD44D44
DDDDDD4DD4D4DD44
DDD4D44DDDDDDDD4
DD44DDD4DD4DD444
D444DDD44D44DD4D
4DDDD4DD44444DDD
4DDD4DDDDDDDD444
44DD44D4DDD4DDD4
DD44DD4D4D444DD4`],
  [ gem, bitmap `
................
................
................
................
....00000000....
...0777777770...
..077777722770..
..077777772770..
...0777777770...
....07777770....
.....077770.....
......0770......
.......00.......
................
................
................`],
  [ ship, bitmap `
7777777777777777
7777777C77777777
7777700000077777
7777022222107777
7770222221077777
7770222221077777
7770222221077777
7777022222107777
7777700000070777
7070777070777077
0000007070700007
7066660000066607
770CCC66666CCC07
770CCCCCCCCCC077
7770CCCCCCCC0777
7777000000007777`],
  [ groundfloor, bitmap `
7777777777777777
7777777777777777
7777777777777777
7777777777777777
7777777777777777
7777777777777777
7777777777777777
7777777777777777
7777777777777777
7777777777777777
7777777777777777
7777777777777777
7777777777777777
7777777777777777
7777777777777777
7777777777777777`],
  [ groundsoil, bitmap `
CCCCCCCCCCCCCCCC
CCCCCCCCCCCCCCCC
CCCCCCCCCCCCCCCC
CCCCCCCCCCCCCCCC
CCCCCCCCCCCCCCCC
CCCCCCCCCCCCCCCC
CCCCCCCCCCCCCCCC
CCCCCCCCCCCCCCCC
CCCCCCCCCCCCCCCC
CCCCCCCCCCCCCCCC
CCCCCCCCCCCCCCCC
CCCCCCCCCCCCCCCC
CCCCCCCCCCCCCCCC
CCCCCCCCCCCCCCCC
CCCCCCCCCCCCCCCC
CCCCCCCCCCCCCCCC`],
  [ sand, bitmap `
6666666666666666
6616666666662666
6666666266666666
6666266666666666
6666666666666666
6266666626666666
6666666666666666
6666666666626662
6666666166666666
6662666666666616
6666666626666666
6666666666666666
6266666666666666
6666666666662666
6666616666666666
6662666666266666`],
  [ sandtosoil, bitmap`
6666666666666666
66L6666666666666
66666666666L6666
6666662666666666
6666666666666666
66666666666266L6
6666666666666666
666L666666666666
6666666666666666
C66266666L666666
CC66CCCCC666666C
CCCCCCCCCCC66CCC
CCCCCCCCCCCCCCCC
CCCCCCCCCCCCCCCC
CCCCCCCCCCCCCCCC
CCCCCCCCCCCCCCCC`],
  [ crate, bitmap `
................
.00000000000000.
.0110CCCCCC0110.
.01CC0CCCC0CC10.
.00CCC0CC0CCC00.
.0C0CCC00CCC0C0.
.0CC0CCC0CC0CC0.
.0CCC0CCC00CCC0.
.0CCC00CCC0CCC0.
.0CC0CC0CCC0CC0.
.0C0CCC00CCC0C0.
.00CCC0CC0CCC00.
.01CC0CCCC0CC10.
.0110CCCCCC0110.
.00000000000000.
................`],
  [ oceanBridge, bitmap `
777CCCCCCCCCC777
777CCCCCCCCCC777
7771CCCCCCCC1777
777CCCCCCCCCC777
777CCCCCCCCCC777
7771CCCCCCCC1777
777CCCCCCCCCC777
777CCCCCCCCCC777
7771CCCCCCCC1777
777CCCCCCCCCC777
777CCCCCCCCCC777
7771CCCCCCCC1777
777CCCCCCCCCC777
777CCCCCCCCCC777
7771CCCCCCCC1777
777CCCCCCCCCC777`]
)

let level = 0;
const levels = [
  map`
b..b
pg.b
bb..
bs..`,
  map `
sddd
fttt
bbgb
p..b`,
  map `
p....b
dbbbgb
ddsb.b
btbb..
b.....
b..bbb`,
  map `
p.c.
bbgb
dd.b
ttsb`,
  map `
b..bb
.c..b
cpgc.
.c...
s..bb`,
  map `
tsbbb
.....
bcbg.
b.b.p
b...b`,
  map `
ddsbbb
bb.gc.
b.c...
bbb..p`,
  map `
bsbbb
.c...
.cgc.
bpb..
.....`,
  map `
b...
pg..
bc..
...b
bsbb`,
  map `
bsbb
bobd
bgtt
pc..`,
  
]

const currentLevel = levels[level];
setMap(currentLevel);


setBackground(groundsoil);


setSolids([ player, gem, block, groundfloor, crate ]); // other sprites cannot go inside of these sprites


setPushables({
  [ player ]: [ gem, crate ],
  [ crate ]: [ crate, gem]
})




// inputs for player movement control
onInput("s", () => {
  getFirst(player).y += 1; // positive y is downwards
});

onInput("d", () => {
  getFirst(player).x += 1;
});

onInput("w", () => {
  getFirst(player).y -= 1;
});

onInput("a", () => {
  getFirst(player).x -= 1;
});

// input to reset level
onInput("j", () => {
  const currentLevel = levels[level]; // get the original map of the level

  // make sure the level exists before we load it
  if (currentLevel !== undefined) {
    clearText("");
    setMap(currentLevel);
  }
});


// these get run after every input
afterInput(() => {
  setTimeout(() => {
    nextLevel();
  }, 1500);

  addText("Level " + (level + 1), { y: 0, color: color`2` });



});

function nextLevel() {
    // count the number of tiles with ships
  const targetNumber = tilesWith(ship).length;
  
  // count the number of tiles with ships and gemes
  const numberCovered = tilesWith(ship, gem).length;



  // if the number of ships is the same as the number of ships covered
  // all goals are covered and we can go to the next level
  if (numberCovered === targetNumber) {

    // increase the current level number
    level = level + 1;

    const currentLevel = levels[level];



    // make sure the level exists and if so set the map
    // otherwise, we have finished the last level, there is no level
    // after the last level
    if (currentLevel !== undefined) {
      setMap(currentLevel);
      playTune(gemSound);

    } else {
      addText("Congrats! you win!", { y: 4, color: color`2` });

      playTune(winTune);
      playback.end();
      
    }

    
  }
}
